package model.sit
{
	import events.SitEvent;
	
	import flash.events.EventDispatcher;

	public class SitModel extends EventDispatcher
	{
		private var sitDatas:Array;
		private var sitAddEvent:SitEvent;
		private var sitStateChangeEvent:SitEvent;
		private var removeAddEvent:SitEvent;
		private var moneyChangeEvent:SitEvent;
		private var sitTalkEvent:SitEvent;
		private var sitFaceEvent:SitEvent;
		private var sitItemAnimateEvent:SitEvent;
		
		
		public function SitModel()
		{
			initEvents();
			initSitDatas();
		}
		
		private function initEvents():void
		{
			
		
		}
		
		private function initSitDatas():void
		{
			sitDatas = [];
		}
		
		public function updateSitItemAnimate(sitId:String,animateId:String):void
		{
			sitItemAnimateEvent = new SitEvent(SitEvent.SIT_ITEM_ANIMATE);
			sitItemAnimateEvent.sitId = int(sitId);
			sitItemAnimateEvent.animateId = animateId;
			dispatchEvent(sitItemAnimateEvent);
		}
		
		public function updateSitState(sitState:ISitState):void
		{
			sitStateChangeEvent = new SitEvent(SitEvent.STATE_CHANGE);
			sitStateChangeEvent.sitState = sitState;
			dispatchEvent(sitStateChangeEvent);
		}
		
		public function updateSitMoney(sitId:int,money:String):void
		{
			moneyChangeEvent = new SitEvent(SitEvent.MONEY_CHANGE);
			moneyChangeEvent.sitId = sitId;
			moneyChangeEvent.money = money;
			
			var sitData:ISitData = getSitDataById(sitId);
			if (sitData)
			{
				sitData.roleData.money = money;
			}
			
			dispatchEvent(moneyChangeEvent);
		}
		
		public function updateSitAutoState(sitId:int,state:int):void
		{
			var event:SitEvent = new SitEvent(SitEvent.SIT_AUTO_STATE);
			event.sitId = sitId;
			event.autoState = state;
			dispatchEvent(event);
		}
		
		public function updateSitTalk(sitId:int,content:String):void
		{
			sitTalkEvent = new SitEvent(SitEvent.SIT_TALK);
			sitTalkEvent.sitId = sitId;
			sitTalkEvent.content = content;
			dispatchEvent(sitTalkEvent);
		}
		
		public function updateFace(sitId:int,faceKey:String):void
		{
			sitFaceEvent = new SitEvent(SitEvent.SIT_EMOTE);
			sitFaceEvent.sitId = sitId;
			sitFaceEvent.emoteKey = faceKey;
			dispatchEvent(sitFaceEvent);
		}
		
		public function updateMoneyChangeAnimate(sitId:int,value:int):void
		{
			var event:SitEvent = new SitEvent(SitEvent.SIT_MONEY_CHANGE_ANIMATION);
			event.sitId = sitId;
			event.money = String(value);
			
			
			
			dispatchEvent(event);
		}
		
		public function addSit(sitData:ISitData):void
		{
			sitDatas.push(sitData);
			sitAddEvent = new SitEvent(SitEvent.ADD);
			sitAddEvent.sitData = sitData;
			dispatchEvent(sitAddEvent);
		}
		
		public function getSitDataById(sitId:int):ISitData
		{
			for each (var sitData:ISitData in sitDatas)
			{
				if (sitData.id == sitId)
				{
					return sitData;
				}
			}
			return null;
		}
		
		public function removeSit(sitId:int):void
		{
			removeAddEvent = new SitEvent(SitEvent.REMOVE);	
			var sitData:ISitData;
			for (var i:int=0;i<sitDatas.length; i++)
			{
				sitData = sitDatas[i];
				if (sitData.id == sitId)
				{
					removeAddEvent.sitData = sitDatas.splice(i,1)[0];
					dispatchEvent(removeAddEvent);
					return;
				}
			}
		}
		
		public function sitAnimation(fromSitId:int,toSitId:int,animateId:int):void
		{
			var event:SitEvent = new SitEvent(SitEvent.SIT_GIVE_GIFT);
			event.fromId = fromSitId;
			event.toId = toSitId;
			event.animateId = animateId.toString();
			dispatchEvent(event);
		}
		
		
		public function updateSitInfo(data:SitInfoData):void
		{
			var event:SitEvent = new SitEvent(SitEvent.SIT_INFO_DETAIL);
			event.sitInfoData = data;
			dispatchEvent(event);
		}
		
		public function clear():void
		{
			while(sitDatas.length > 0)
			{
				removeSit(sitDatas[0].id);
			}
		}
		
		
	}
}